﻿@model StackExchange.Opserver.Views.Elastic.DashboardModel.CurrentData
@{
    Layout = null;
    var c = Model.Cluster;
    var hs = c.HealthStatus.SafeData(true);
    var indices = (Model.WarningsOnly ? c.TroubledIndexes : c.HealthStatus.Data.Indices).ToList();
    var stats = c.Stats.SafeData(true);
}
@if (indices.Any())
{
    <table class="node-dashboard cluster-dashboard">
        <tbody class="node-header" data-name="header-indexes-@c.UniqueKey-@hs.Name">
            <tr class="category-row">
                <th colspan="10">
                    <h3>
                        @c.IconSpan() @indices.Count().ToComma() Indexes @(Model.WarningsOnly ? "in trouble" : "") on @hs.TotalNodeCount.Pluralize("node") (@hs.Name)
                        <span class="top-right-info">Updated @c.Stats.ToPollSpan()</span>
                    </h3>
                </th>
            </tr>
            <tr>
                <th></th>
                <th>Name</th>
                <th>Searches <span class="note">(cur)</span></th>
                <th>Docs <span class="note">(deleted)</span></th>
                <th>Size</th>
                <th>Shards <span class="note" title="Replicas">(repl)</span></th>
                <th>Active <span class="note" title="Primary Active Shards">(pri)</span></th>
                <th title="Initializing">Init</th>
                <th title="Relocating">Rel</th>
                <th title="Unassigned">Un</th>
            </tr>
        </tbody>
        <tbody class="node-group" data-name="indexes-@c.UniqueKey-@hs.Name">
            @foreach (var i in indices.OrderByWorst().ThenBy(i =>
                {
                    int j;
                    return int.TryParse(i.Name, out j) ? j : 0;
                }).ThenBy(i => i.Name))
            {
                var iStats = stats.GetIndexStats(i.Name);
                <tr class="@i.RowClass()">
                    <td>@i.IconSpan()</td>
                    <td>@c.GetIndexAliasedName(i.Name)</td>
                    @if (iStats != null)
                    {
                        <td>@(iStats.Total.Search.QueryTotal.ToComma()) <span class="note">(@(iStats.Total.Search.QueryCurrent.ToComma()))</span></td>
                        <td>@(iStats.Total.Documents.Count.ToComma()) <span class="note">(@(iStats.Total.Documents.Deleted.ToComma()))</span></td>
                        <td>@(iStats.Total.Store.SizeInBytes.ToSize("b"))</td>
                    }
                    else
                    {
                        <td colspan="3" class="note">(no stats)</td>
                    }
                    <td>@i.NumberOfShards.ToComma() <span class="note">(@i.NumberOfReplicas.ToComma())</span></td>
                    <td>@i.ActiveShards.ToComma() <span class="note">(@i.ActivePrimaryShards.ToComma())</span></td>
                    <td>@i.InitializingShards.ToComma()</td>
                    <td>@i.RelocatingShards.ToComma()</td>
                    <td>@i.UnassignedShards.ToComma()</td>
                </tr>
            }
            @if (!Model.WarningsOnly)
            {
                var indexDict = stats.GetIndexStats();
                var indexStats = indexDict != null ? indexDict.Values : null;
                <tr class="total-row">
                    <td></td>
                    <td><b>Total</b></td>
                    @if (indexStats != null)
                    {
                        <td>@(indexStats.Sum(i => i.Total.Search.QueryTotal).ToComma()) <span class="note">(@(indexStats.Sum(i => i.Total.Search.QueryCurrent).ToComma()))</span></td>
                        <td>@indexStats.Sum(i => i.Total.Documents.Count).ToComma() <span class="note">(@indexStats.Sum(i => i.Total.Documents.Deleted).ToComma())</span></td>
                        <td>@indexStats.Sum(i => i.Total.Store.SizeInBytes).ToSize("b")</td>
                    }
                    else
                    {
                        <td colspan="3" class="note">(no stats)</td>
                    }
                    <td>@indices.Sum(i => i.NumberOfShards)  <span class="note">(@indices.Sum(i => i.NumberOfReplicas))</span></td>
                    <td>@indices.Sum(i => i.ActiveShards) <span class="note">(@indices.Sum(i => i.ActivePrimaryShards))</span></td>
                    <td>@indices.Sum(i => i.InitializingShards)</td>
                    <td>@indices.Sum(i => i.RelocatingShards)</td>
                    <td>@indices.Sum(i => i.UnassignedShards)</td>
                </tr>
            }
        </tbody>
    </table>
}
else if(!Model.WarningsOnly)
{
    <div class="no-content">No indexes found</div>
}